package com.handpay.nocardpay.baselibrary.notification.presenter;

import android.content.Context;

import com.handpay.nocardpay.baselibrary.commbase.model.IBaseModel;
import com.handpay.nocardpay.baselibrary.commbase.model.bean.BaseErrorInfo;
import com.handpay.nocardpay.baselibrary.db.ormlite.DataBaseModel;
import com.handpay.nocardpay.baselibrary.logutils.HPLog;
import com.handpay.nocardpay.baselibrary.notification.model.impl.NoticeModelImpl;
import com.handpay.nocardpay.baselibrary.notification.model.bean.NoticeBody;
import com.handpay.nocardpay.baselibrary.notification.model.bean.UserNoticeBean;
import com.handpay.nocardpay.baselibrary.notification.view.INotice;
import com.handpay.nocardpay.baselibrary.notification.view.INoticeListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by fplei on 2017/8/9.
 */

public class NoticePresenterImpl implements NoticePresenter {
    private NoticeModelImpl noticeModel;
    private INoticeListView iNoticeListView;
    private DataBaseModel dataBaseModel;
    private INotice iNotice;

    public NoticePresenterImpl(INoticeListView miNoticeListView, Context context) {
        this.noticeModel = new NoticeModelImpl(context);
        this.iNoticeListView = miNoticeListView;
        this.dataBaseModel = DataBaseModel.getInstance(context);
    }

    public NoticePresenterImpl(INotice miNotice, Context context) {
        this.noticeModel = new NoticeModelImpl(context);
        this.iNotice = miNotice;
        this.dataBaseModel = DataBaseModel.getInstance(context);
    }

    //往服务器查询
    @Override
    public void queryNotices(HashMap<String, Object> paramers, boolean isShowLoading) {
        showLoading(isShowLoading);
        noticeModel.queryNotices(paramers,UserNoticeBean.class, new IBaseModel.OnRequestFinishedListener<UserNoticeBean>() {
            @Override
            public void onError(BaseErrorInfo baseErrorInfo) {
                hiddenLoading();
                if (iNoticeListView != null) {
                    iNoticeListView.showErrorAlert(baseErrorInfo);
                }
            }
            @Override
            public void onSuccess(UserNoticeBean response) {
                hiddenLoading();
                if (iNoticeListView != null) {
                    iNoticeListView.showNotices(response);
                }
                if (iNotice != null) {
                    iNotice.showNotices(response);
                }
            }
        });
    }
    private void showLoading(boolean isShowLoading){
        if (iNoticeListView != null) {
            if (isShowLoading) iNoticeListView.showProgress();
        }
    }
    private void hiddenLoading(){
        if (iNoticeListView != null) {
            iNoticeListView.hideProgress();
        }
    }
    @Override
    public void addNotice(NoticeBody noticeBody) {
        if (dataBaseModel != null) {
            dataBaseModel.addNotice(noticeBody);
        }
    }

    @Override
    public void updateNotice(NoticeBody noticeBody) {
        if (dataBaseModel != null) {
            dataBaseModel.updateNotice(noticeBody);
        }
    }

    @Override
    public void queryNoticesfromDB(String userId) {
        String temp_id = userId;
        if (dataBaseModel != null) {
            List<NoticeBody> noticeBodyList = queryNotice(temp_id);
            if (iNoticeListView != null) {
                iNoticeListView.showNoticesWithDB(noticeBodyList);
            }
        }
    }

    @Override
    public void deleteNotice(NoticeBody noticeBody) {
        if (dataBaseModel != null) {
            dataBaseModel.deleteNotice(noticeBody);
        }
    }

    public boolean isReadNotice(String userId) {
        List<NoticeBody> noticeBodies = new ArrayList<>();
        for (NoticeBody noticeBody : queryNotice(userId)) {
            if (!noticeBody.isRead()) {
                noticeBodies.add(noticeBody);
            }
        }
        HPLog.i(HPLog.LFP_TAG, "没读的消息：" + noticeBodies.size());
        return noticeBodies.size() > 0;
    }

    private List<NoticeBody> queryNotice(String userId) {
        if (dataBaseModel != null) {
            List<NoticeBody> noticeBodyList = dataBaseModel.queryNotices(userId);
            return noticeBodyList;
        }
        return null;
    }

    public  void deleteNotice(String userId,long limit){
        if (dataBaseModel != null) {
            dataBaseModel.deleteNotice(userId,limit);
        }
    }

    @Override
    public void onDestory() {
        iNoticeListView = null;
        noticeModel = null;
    }


    private UserNoticeBean createTestData() {
        List<NoticeBody> noticeBodies = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            NoticeBody body = new NoticeBody();
            body.setTitle("这是标题！");
            body.setLastUpdateTime("20170801184510");
            body.setContent("这是测试模拟数据，请勿在在意，哈哈哈哈哈哈哈哈哈啊哈哈哈哈啊啊哈哈哈哈" + i);
            body.setNoticeId("1" + i);
            body.setRead(false);
            body.setTime("2017080118450" + i);
            body.setUserId("19645238");
            noticeBodies.add(body);
        }
        UserNoticeBean mr = new UserNoticeBean();
        mr.setItems("2");
        mr.setLastUpdateTime("20170801184510");
        mr.setList(noticeBodies);
        return mr;
    }
}
